package com.security;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.junit.Assert;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class SecurityTest {

    private static final String sky = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKIU6Iu+CsOGs44kQLkzXcH4TgVndzPSm6xpwvtVhhOxWSYxkS1N4tsk2gTp+MUGOg0QUqo/wm/6geI/4/ivifQo6HYZXqCCHP6CnQhJ1eCLTWk/LoZNjf7u0VTPDogurWf+0++Js6CtJNrgqlxkxU/2EyOXco1torHJSkve3fN7AgMBAAECgYAdxid93DhP+MOtPd2h59y2UC3M8lfEblDomDkYQHqzzWVqDbr0jpU42okuU+i+EMnDMpds7O+D8InBjSkMuvplU4x3GdKJL9LAUHbpK/JAoup50AHBmXxdS5TolmIdN9kl+Ix7aBPGSpPt6Hv7h8C8xUkTYEmyV6iNUykpA7MCqQJBAOH4uTqwBp1E+oEzz7pqzV70o7kjzTcCJlsapV1VmqJYge8PUO4IeVjtC7VsBWfC73+CukzhDY75jLW1MHL+nhUCQQC3nrqA3ztzrUr7HK1ucx8kMz75fpmiFTexalfZxkpTJPOiddYo2/J62bRo5A01A3iuv1lAFWNYzf7/lXGQ1D9PAkEAyeB/8rtRnW0DcywLd9p0kVFzdzJR4sPr6lY6x9Bzbngt1JnVTmKJrnVPkYvopFNVzY/HymkAyOuO1sMBfqqUJQJAHUAj5bOp/Nt2PlprJlUgeNvRBF2ui6tlm9Wc+QwleulABnlPDGM9VWSo4KyzjeZnqy7AQP0nPlZScNSVqsSk8QJBAL5WZ1YA3l5a+AfaHjbroM1IPiYq2CmzDfkXmfDTcBsu33ytTSqAPyR1EuNUVvzA5PsuAncYIa3pmq+23wfA+e4=";
    private static final String pky = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiFOiLvgrDhrOOJEC5M13B+E4FZ3cz0pusacL7VYYTsVkmMZEtTeLbJNoE6fjFBjoNEFKqP8Jv+oHiP+P4r4n0KOh2GV6gghz+gp0ISdXgi01pPy6GTY3+7tFUzw6ILq1n/tPvibOgrSTa4KpcZMVP9hMjl3KNbaKxyUpL3t3zewIDAQAB";

    private static final RSA rsa = new RSA(AsymmetricAlgorithm.RSA.getValue(), sky, pky);

    public static void main(String[] args) {
        SecurityTest securityTest = new SecurityTest();
        ConfigUtil.getInstance().init();
        String s = "UNM2000any~!@#$%^&*()_+=-,';/";

//        String strMi = securityTest.encrypt(s);

//        System.out.println("生产新的公钥和私钥【BASE64编码】:");
//        securityTest.generatePublicAndPrivateKey();

        for(int i = 0; i < 2; i++){
            Thread thread = new Thread(()->{
                String strMi = SecurityFactory.getSecurityObject().encrypt(s);
                String strMing = SecurityFactory.getSecurityObject().decrypt(strMi);
                System.out.println("原明文：" + s);
                System.out.println("加密后：" + strMi);
                System.out.println("解密后：" + strMing);
                System.out.println(SecurityFactory.getSecurityObject().hashCode());
            });
            thread.start();
        }
        try {
            Thread.sleep(10000);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成公钥和私钥
     */
    public void generatePublicAndPrivateKey() {
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA.getValue());
        //获得私钥
        PrivateKey privateKey = rsa.getPrivateKey();
        String sk = rsa.getPrivateKeyBase64();
        //获得公钥
        PublicKey publicKey = rsa.getPublicKey();
        String pk = rsa.getPublicKeyBase64();
        System.out.println("私钥：" + sk);
        System.out.println("公钥：" + pk);
    }

    /**
     * 使用公钥对指定字符串进行加密
     *
     * @param strMing 待加密的字符串
     * @return 加密后的字符串，如果返回null，则表示加密失败
     */
    public String encrypt(String strMing) {
        byte[] bytesMi = rsa.encrypt(strMing, CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
        String strMi = Base64.getEncoder().encodeToString(bytesMi);
        return strMi;
    }

    /**
     * 使用私钥对指定字符串进行解密
     *
     * @param strMi 待解密的字符串
     * @return 解密后的字符串，如果返回null，则表示解密失败
     */
    public String decrypt(String strMi) {
        byte[] bytesMing = rsa.decrypt(Base64.getDecoder().decode(strMi), KeyType.PrivateKey);
        String strMing = new String(bytesMing, CharsetUtil.CHARSET_UTF_8);
        return strMing;
    }


}
